<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/5/11
 * Time: 9:20
 */

namespace SDXApi\Service;


use SDXApi\Model\CommunityAuthorModel;
use SDXApi\Model\CommunityLikeModel;
use SDXApi\Model\CommunityModel;
use SDXApi\Model\GoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class CommunityService
 * @package SDXApi\Service
 */
class CommunityService
{
    /**
     * @var CommunityModel
     */
    protected $communityModel;

    /**
     * @var CommunityLikeModel
     */
    protected $communityLikeModel;

    /**
     * @var CommunityAuthorModel
     */
    protected $communityAuthorModel;

    /**
     * @var GoodsModel
     */
    protected $goodsModel;

    /**
     * @var GoodsRepository
     */
    protected $goodsRepository;

    /**
     * CommunityService constructor.
     */
    public function __construct()
    {
        $this->communityModel = new CommunityModel();
        $this->communityLikeModel = new CommunityLikeModel();
        $this->communityAuthorModel = new CommunityAuthorModel();
        $this->goodsModel = new GoodsModel();
        $this->goodsRepository = new GoodsRepository();
    }

    /**
     * Created by Jerry
     * @param int $id
     * @param int $uid
     * @param int $wid
     * @return mixed
     */
    public function getArticleDetail($id = 0, $uid = 0, $wid = 8)
    {
        $article =  $this->communityModel->findCommunityInfo($id);
        $this->goodsRepository->userId = $uid;
        $this->goodsRepository->wid = $wid;

        $article['content'] = $this->setContentImageHost($article['content']);
        $article['content'] = $this->setContentImageHttps($article['content']);
        $article['content'] = $this->setContentWImageHttps($article['content']);
        $article['content'] = $this->getContentGoodsId($article['content'], $wid);
        $article['content'] = $this->setContentStyle($article['content']);

        $article['add_time'] = date("Y年m月d日", $article['add_time']);

        $count = $this->communityLikeModel->getCount($id, $uid);
        if ($count > 0) {
            $article['like'] = '1';
        } else {
            $article['like'] = '0';
        }

        $article['author_'] = '0';

        $this->communityModel->setCommunityInc('reading_times', $id);
        $this->communityAuthorModel->setCommunityAuthorInc('reading_times', $id);

        return $article;
    }

    /**
     * Created by Jerry
     * @param null $content
     * @param int $wid
     * @return bool|mixed|null
     */
    protected function getContentGoodsId($content = null, $wid = 8)
    {
        $replace = '/{{ goods_id=([0-9]+) }}/';
        preg_match_all($replace, $content, $goods);

        if (empty($goods)) {
            return false;
        }

        $goods_ids = $goods[1];

        foreach ($goods_ids as $key => $value) {
            $result = $this->getGoodsInfo($value, $wid);

            $str = "{{ goods_id={$value} }}";

            if (empty($result)) {
                $content = str_replace($str, '', $content);
            }


            $content = str_replace($str, $result, $content);
        }

        return $content;
    }

    /**
     * Created by Jerry
     * @param $id
     * @param $wid
     * @return bool|string
     */
    protected function getGoodsInfo($id, $wid)
    {
        $goods = $this->goodsModel->getArticleGoods($id, $wid);
        $goodsInfo = $this->goodsRepository->formatGoodsArticleTransform($goods);

        if (empty($goodsInfo)) {
            return false;
        }

        return $this->goodsTemplate($goodsInfo);
    }

    /**
     * Created by Jerry
     * @param array $goods
     * @return string
     */
    protected function goodsTemplate($goods = array())
    {
        $html = <<<FOO
        <a href="https://#!/info?id={$goods['goods_id']}">
        <div style="height: 3.3rem;border-bottom: .5px solid #dedede;background: #fff;position: relative;border-top: 1px solid #fff;margin-bottom:0;overflow:hidden;">
		<div style="float:left;width: 3.3rem;height: 3.3rem;position: relative;top: 0;left: 0;">
			<img src="https://image.shidexian.com/{$goods['original_img']}" style="width:3.3rem;height:3.3rem;">
		</div>
		<div style="margin-left: 3.6rem;height: 100%;position: relative;margin-right: 14px;">
			<p style="margin: .1rem 0;white-space:nowrap;overflow: hidden;text-overflow: ellipsis;line-height: .44rem;margin-top: 0;top: .35rem;font-size: .4rem;color: #393939;position: absolute;left: 0;width: 100%;">{$goods['goods_name']}</p>
			<p style="white-space:nowrap;overflow: hidden;text-overflow: ellipsis;margin-bottom: .03rem;position: absolute;left: 0;width: 100%;font-size: .32rem;color: #777;line-height: .48rem;top: .826rem;">{$goods['goods_brief']}</p>
			<p style="font-size:.266rem;bottom: 1rem;line-height:.4rem;margin-top: 0;top: 1.3rem;color: #ff4463;position: absolute;left: 0;">
				<span style="display:inline-block;background:#fff;margin-right: 3px;border: .5px solid #75a342;border-radius: .43rem;color: #75a342;height: .42rem;line-height: .42rem;padding: 0 4px;font-size: .26rem;">1个</span>
				<span style="display:inline-block;background:#fff;margin-right: 3px;border: .5px solid #75a342;border-radius: .43rem;color: #75a342;height: .42rem;line-height: .42rem;padding: 0 4px;font-size: .26rem;">起够数量2份</span>
			</p>
		</div>
		<div style="position: absolute;left: 0;display: block;width: 100%;padding-left: 3.6rem;    bottom: 0;border-bottom: 0;background: none;height: .92rem;top: 2.3rem;">
			<div style="border-top:0;">
				<div style="left: 3.6rem;color: #ff5870;vertical-align: baseline;z-index:1;bottom: 0;top: auto;padding-top: 0;position: static;">
					<div style="display:inline-block;font-size:.37rem;color: #ff4562;">￥</div>
					<div style="position: relative;top: 1px;display: inline-block;margin-left:-4px;font-size: .4rem;line-height: .53rem;color: #ff4562;">{$goods['market_price']}</div>
					<div style="margin-left: 1px;text-decoration: none;display: none;font-size: .29rem;line-height: .346rem;position: static;margin-top: 0;">￥{$goods['vip_price']}</div>
					<div style="margin-left:1px;color:#393939;text-decoration: none;display:block;font-size: .29rem;line-height: .346rem;position: static;margin-top: 0;">VIP会员价￥{$goods['true_vip_price']}</div>
				</div>
			</div>
		</div>
	</div>
    </a>
FOO;

        return $html;

    }

    /**
     * Created by Jerry
     * @param null $content
     * @return bool|mixed
     */
    protected function setContentImageHost($content = null)
    {
        if (empty($content)) {
            return false;
        }

        $thisUrl = '';
        switch ($_SERVER['SERVER_NAME']) {
            case 'api.xian3.com':
                $thisUrl = 'image.xian3.com';
                break;
            case 'api.shidexian.dev':
                $thisUrl = 'image.shidexian.dev';
                break;
            case 'api.test.shidexian.com':
                $thisUrl = 'image.shidexian.com';
                break;
            case 'api.shidexian.com':
                $thisUrl = 'image.shidexian.com';
                break;
            default:
                $thisUrl = 'image.shidexian.com';
                break;
        }

        return preg_replace('/(<img.+?src=")(\/.*?)/', '$1https://' . $thisUrl . '$2', $content);
    }

    /**
     * Created by Jerry
     * @param null $content
     * @return mixed
     */
    protected function setContentImageHttps($content = null)
    {
        $data = str_replace('http:', 'https:', $content);
        return $data;
    }

    /**
     * Created by Jerry
     * @param null $content
     * @return mixed
     */
    protected function setContentWImageHttps($content = null)
    {
        $data = str_replace('w.shidexian', 'image.shidexian', $content);
        return $data;
    }

    /**
     * @author Jerry
     * @param null $content
     * @return mixed|null
     */
    protected function setContentStyle($content = null)
    {
        $content = preg_replace('/(<img.+)(!important+)/', '$1', $content);
        $content = preg_replace('/(<section .+)(padding-right:2px;+)/', '$1', $content);
        $content = preg_replace('/(<section .+)(display:inline-block;+)/', '$1', $content);
        $content = preg_replace('/(<section .+)(width:50%;+)/', '$1', $content);
        return $content;
    }
}